/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SimpleTable.java
 *
 * Created on 24-06-2010, 19:55:51
 */
package gruppe1projekt.GUILayer.prototypes;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Template;
import gruppe1projekt.GUILayer.BaseWindow;
import gruppe1projekt.ModelLayer.Price;
import gruppe1projekt.ModelLayer.Product;
import java.lang.reflect.Method;
import java.util.ArrayList;
import javax.swing.table.*;

/**
 *
 * @author Moulde
 */
public class SimpleTable<T> extends BaseWindow
    {

    DefaultTableModel model;
    ArrayList<T> items;
    ArrayList<String> Columns;
    int columnCount = 0;

    /** Creates new form SimpleTable */
    public SimpleTable(ArrayList<T> items)
    {
        initComponents();
        
        if (items.size() <= 0)
            return;

        this.items = items;
        GenerateColumns(items);
        renewData();

        table.setModel(model);
    }

    private void GenerateColumns(ArrayList<T> items)
    {
        ArrayList<String> colNames = new ArrayList<String>();

        T item = items.get(0);
        for (Method method : item.getClass().getMethods())
        {
            if (method.isAnnotationPresent(IncludeInColumn.class))
            {
                IncludeInColumn annotation = method.getAnnotation(IncludeInColumn.class);
                String colName = annotation.ColumnName();
                colNames.add(colName);
            }
        }

        columnCount = colNames.size();
        Columns = colNames;
    }

    private void renewData()
    {
        ArrayList<Object[]> data = new ArrayList<Object[]>();
        for (T item : items)
        {
            ArrayList<String> tempData = new ArrayList<String>();
            for (Method method : item.getClass().getMethods())
            {
                if (method.isAnnotationPresent(IncludeInColumn.class))
                {
                    IncludeInColumn annotation = method.getAnnotation(IncludeInColumn.class);
                    Class type = method.getReturnType();
                    try
                    {
                        tempData.add(type.cast(method.invoke(item, null)).toString());
                    } catch (Exception e)
                    {
                    }
                }
            }
            data.add(tempData.toArray());
        }
        
        model = new DefaultTableModel(ConvertTo2D(data), Columns.toArray());
    }

    private Object[][] ConvertTo2D(ArrayList<Object[]> items)
    {
        Object[][] data = new Object[items.size()][columnCount];

        for (int i = 0; i < items.size(); i++)
        {
            for (int j = 0; j < items.get(i).length; j++)
            {
                data[i][j] = items.get(i)[j];
            }
        }

        return data;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(table);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
    }
